var app = getApp();

const db = wx.cloud.database({
  throwOnNotFound: false
})
const _ = db.command

Page({
  data: {
    active: 0,
    price: 0,
    myTaskList: [],
    myExchangeList: []
  },

  onLoad: function () {
    //获取所有未审核的志愿数据
    wx.cloud.callFunction({
      name: "getAllTaskList",
      complete: res => {
        console.log("getAllTaskList成功", res)
        this.setData({
          myTaskList: res.result.data,

        })
      }
    });

    //获取所有待审核的兑换数据
    wx.cloud.callFunction({
      name: "getAllExchangeList",
      complete: res => {
        console.log("getAllExchangeList成功", res)
        this.setData({
          myExchangeList: res.result.data,
        })
      }
    });
  },

  onShow: function () {
    wx.cloud.callFunction({
      name: "getAllTaskList",
      complete: res => {
        console.log("onshow,getAllTaskList成功", res)
        this.setData({
          myTaskList: res.result.data
        })
      }
    });
    wx.cloud.callFunction({
      name: "getAllExchangeList",
      complete: res => {
        console.log("onSHow-getAllExchangeList成功", res)
        this.setData({
          myExchangeList: res.result.data,
        })
      }
    });
  },
  //获取用户输入
  bindNumInput(e) {
    var price = Number(e.detail.value);
    this.setData({
      price: price
    })
  },


  //注意更新非本人的所有数据都需要使用云函数
  //志愿通过按钮
  passConfirm: function (e) {
    console.log("点击通过")
    const id = e.currentTarget.dataset.id;
    const userID = e.currentTarget.dataset.userid;
    console.log("用户的id和userID为", e, id, userID)
    if (isNaN(this.data.price) || this.data.price == "") {
      wx.showToast({
        title: "请输入正确的金额",
        icon: 'none'
      })
    } else {
      wx.showModal({
        title: '通过确认',
        content: '确认要通过吗？',
        success: res => {
          if (res.confirm) {
            console.log('点击确认回调')

            //更新用户时间货币和服务次数+1，待审核志愿-1，已完成志愿+1

            wx.cloud.callFunction({
              name: "passUpdateUser",
              data: {
                userID: userID,
                timecurrency: this.data.price
              },
              complete: res => {
                console.log("用户数据更新成功", res)

              }
            });

            //更新报名志愿的状态,status:0->1
            wx.cloud.callFunction({
              name: "passUpdateTask",
              data: {
                id: id
              },
              complete: res => {
                console.log("此条志愿数据更新成功", res)
                //更新页面
                //放在complete里面，防止异步调用，次函数先执行，如此无法更新页面
                wx.cloud.callFunction({
                  name: "getAllTaskList",
                  complete: res => {
                    console.log("更新页面getAllTaskList成功", res)
                    this.setData({
                      myTaskList: res.result.data
                    })
                  }
                });

              }
            });



            wx.showToast({
              title: '已通过',
            })
          } else {
            console.log('点击取消回调')
          }
        }
      })

    }



  },

  //志愿退回按钮
  refuseConfirm: function (e) {
    console.log("点击退回")
    const _id = e.currentTarget.dataset.id;
    const userID = e.currentTarget.dataset.userid;
    console.log("用户的id和userID为", e, _id, userID)
    wx.showModal({
      title: '退回确认',
      content: '确认要退回吗？',
      success: res => {
        if (res.confirm) {
          console.log('点击确认回调')

          //更新用户时间货币和服务次数+1，待审核志愿-1，已完成志愿+1
          wx.cloud.callFunction({
            name: "refuseUpdateUser",
            data: {
              userID: userID,
            },
            complete: res => {
              console.log("用户数据更新成功", res)

            }
          });

          //更新报名志愿的状态,status:0->2 表示未通过

          wx.cloud.callFunction({
            name: "refuseUpdateTask",
            data: {
              id: _id
            },
            complete: res => {
              console.log("此条志愿数据更新成功", res)
              //更新页面

              wx.cloud.callFunction({
                name: "getAllTaskList",
                complete: res => {
                  console.log("更新页面getAllTaskList成功", res)
                  this.setData({
                    myTaskList: res.result.data
                  })
                }
              });
            }
          });




          wx.showToast({
            title: '已退回',
          })
        } else {
          console.log('点击取消回调')
        }
      }
    })

  },

  //兑换通过按钮
  exchangePass: function (e) {
    console.log("点击通过")
    const id = e.currentTarget.dataset.id;
    const userID = e.currentTarget.dataset.userid;
    console.log("用户的id和userID为", e, id, userID)

    wx.showModal({
      title: '通过确认',
      content: '确认要通过吗？',
      success: res => {
        if (res.confirm) {
          console.log('点击确认回调')

          //更新用户，待审核兑换-1，已完成兑换+1

          wx.cloud.callFunction({
            name: "exchangeUpdateUser",
            data: {
              userID: userID,
            },
            complete: res => {
              console.log("用户数据更新成功", res)

            }
          });

          //更新报名志愿的状态,status:0->1
          wx.cloud.callFunction({
            name: "passUpdateExchange",
            data: {
              id: id
            },
            complete: res => {
              console.log("此条志愿数据更新成功", res)
              //更新页面
              //放在complete里面，防止异步调用，次函数先执行，如此无法更新页面
              wx.cloud.callFunction({
                name: "getAllExchangeList",
                complete: res => {
                  console.log("更新页面getAllExchangeList成功", res)
                  this.setData({
                    myExchangeList: res.result.data
                  })
                }
              });

            }
          });



          wx.showToast({
            title: '已通过',
          })
        } else {
          console.log('点击取消回调')
        }
      }
    })





  },


  //审核退回按钮
  exchangeRefuse: function (e) {
    console.log("点击退回")
    const _id = e.currentTarget.dataset.id;
    const userID = e.currentTarget.dataset.userid;
    console.log("用户的id和userID为", e, _id, userID)
    wx.showModal({
      title: '退回确认',
      content: '确认要退回吗？',
      success: res => {
        if (res.confirm) {
          console.log('点击确认回调')

          //更新用户未完成兑换+1
          wx.cloud.callFunction({
            name: "exchangeRefuseUser",
            data: {
              userID: userID,
            },
            complete: res => {
              console.log("用户数据更新成功", res)

            }
          });

          //更新报名志愿的状态,status:0->2 表示未通过

          wx.cloud.callFunction({
            name: "refuseUpdateExchange",
            data: {
              id: _id
            },
            complete: res => {
              console.log("此条志愿数据更新成功", res)
              //更新页面

              wx.cloud.callFunction({
                name: "getAllExchangeList",
                complete: res => {
                  console.log("更新页面getAllExchangeList成功", res)
                  this.setData({
                    myExchangeList: res.result.data
                  })
                }
              });
            }
          });




          wx.showToast({
            title: '已退回',
          })
        } else {
          console.log('点击取消回调')
        }
      }
    })

  },
})